// --------------------------------------------------------------------------------------------------------------------
// <copyright company="" file="Program.cs">
//   
// </copyright>
// <fileheader>
// Name: Program.cs
// Date: 10/26/2010
// Modified by Vincenzo Panza.
// </fileheader>
// <summary>
//   The program.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Diagnostics;

using BeIT.MemCached;

namespace BenchmarkConsoleApp
{
    using System.Collections.Generic;
    using System.Threading;

    /// <summary>
    /// The program.
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The memory mapped array test.
        /// </summary>
        public class MemoryMappedArrayTest
        {
            /// <summary>
            /// The path.
            /// </summary>
            private static string _path = AppDomain.CurrentDomain.BaseDirectory;

            /// <summary>
            /// The al_.
            /// </summary>
            private static List<int> _al_ = new List<int>();

            /// <summary>
            /// The j.
            /// </summary>
            private static object _j;

            /// <summary>
            /// The rnd.
            /// </summary>
            private static Random _rnd = new Random();

            /// <summary>
            /// The serverlist.
            /// </summary>
            private static string[] _serverlist = { "127.0.0.1:11300" };

            /// <summary>
            /// The mc.
            /// </summary>
            private static MemcachedClient _mc;

            /// <summary>
            /// The init.
            /// </summary>
            [Benchmark]
            public static void init()
            {
                MemcachedClient.Setup("benchmark", _serverlist);
                // Get the instance we just set up so we can use it. You can either store this reference yourself in
                // some field, or fetch it every time you need it, it doesn't really matter.
                _mc = MemcachedClient.GetInstance("benchmark");

                // It is also possible to set up clients in the standard config file. Check the section "beitmemcached" 
                // in the App.config file in this project and you will see that a client called "MyConfigFileCache" is defined.
                // 	MemcachedClient configFileCache = MemcachedClient.GetInstance("MyConfigFileCache");

                // Change client settings to values other than the default like this:
                _mc.SendReceiveTimeout = 5000;
                _mc.ConnectTimeout = 5000;
                _mc.MinPoolSize = 1;
                _mc.MaxPoolSize = 100000;
            }

            /// <summary>
            /// The test insert_ ideal.
            /// </summary>
            [Benchmark]
            public static void TestInsert_Ideal()
            {
                for (int i = 0; i < 10000; i++)
                {
                    int k = _rnd.Next();

                    // if ( !al_.Contains(k) )
                    _al_.Add(k);
                }
            }

            /// <summary>
            /// The test insert_ mmf.
            /// </summary>
            [Benchmark]
            public static void TestInsert_DCache()
            {
                //Stopwatch watch = new Stopwatch();

                /* watch.Reset();
                watch.Start();
                mc.Add("0",1000);
                watch.Stop();
                Console.WriteLine(watch.Elapsed);

                watch.Reset();
                watch.Start();
                mc.Add("1",10000);
                watch.Stop();
                Console.WriteLine(watch.Elapsed); 

              //  Thread.Sleep(1000);
                //watch.Stop();
                Console.WriteLine("get 0: "+mc.Get("0"));
                Console.WriteLine("get 1: " + mc.Get("1")); */
                //watch.Reset();

                for (int i = 0; i < 10000; i++)
                {
                    int k = _rnd.Next();

                    // if(!al.Contains(k))
                    // watch.Start();
                    _mc.Add(i.ToString(), k);
                    // watch.Stop();

                    // Console.WriteLine("k:"+k+";"+i);
                    // j = mc.Get(i.ToString());
                }

                // Console.WriteLine(watch.Elapsed);
            }

            [Benchmark]
            public static void Pause ()
            {
                Thread.Sleep(10000);
            }
            
            [Benchmark]
            public static void TestFetch_Ideal ()
            {
                for ( int i=0; i < 10000; i++ )
                   // if ( al_.Contains(i) )
                    _j = _al_[i];
            }


            [Benchmark]
            public static void TestFetch_DCache ()
            {
                for ( int i=0; i < 10000; i++ )
                   // if ( al.Contains(i) )
                    _j = _mc.Get(i.ToString());

            }
           
            [Benchmark]
            public static void Pause1 ()
            {
                Thread.Sleep(10000);
            }

            /// <summary>
            /// The test remove at_ ideal.
            /// </summary>
            [Benchmark]
            public static void TestRemoveAt_Ideal()
            {
                for ( int i = 0; i < _al_.Count; i++ )
                {
                    _al_.RemoveAt(i);
                }
            }

            /// <summary>
            /// The test remove at_ mmf.
            /// </summary>
            [Benchmark]
            public static void TestRemoveAt_DCache()
            {
                for ( int i = 0; i < 10000; i++ )
                {
                    _mc.Delete(i.ToString());
                }
            }

            /// <summary>
            /// The done_ mmf.
            /// </summary>
            [Benchmark]
            public static void Done_DCache()
            {
                _mc.Dispose();
            }
        }
    }
}